<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="检索" prop="search">
        <el-input v-model="queryParams.search" placeholder="请输入检索信息" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="配送状态" prop="distribution_status">
        <el-select v-model="queryParams.distribution_status" placeholder="请选择配送状态">
          <el-option label="未配送" value="0"></el-option>
          <el-option label="已收货" value="1"></el-option>
          <el-option label="配送中" value="2"></el-option>
        </el-select>
      </el-form-item>
      
      <el-form-item label="配送时间" prop="distributionTime">
        <el-date-picker clearable
          v-model="queryParams.distributionTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择配送时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="到达时间" prop="arriveTime">
        <el-date-picker clearable
          v-model="queryParams.arriveTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择到达时间">
        </el-date-picker>
      </el-form-item>
      
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['carbon:distribution:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['carbon:distribution:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['carbon:distribution:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['carbon:distribution:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="distributionList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column type="index" width="50" label="编号" />
      <el-table-column label="单据号" align="center" prop="orderId" />
      <el-table-column label="调度单号" align="center" prop="dispatchId" />
      <el-table-column label="车牌号" align="center" prop="carNumber" />
      <el-table-column label="司机" align="center" prop="driverName" />
      <el-table-column label="司机联系电话" align="center" prop="driverNumber" />
      <el-table-column label="配送状态" align="center" prop="distributionStatus" >
      <template slot-scope="scope">
          <el-tag effect="dark" type="danger" v-if="scope.row.distributionStatus == '0'">
            配送中
          </el-tag>
          <el-tag effect="dark" type="info" v-else-if="scope.row.distributionStatus == '1'">
            已收货
          </el-tag>
          <el-tag effect="dark" v-else>
            未配送
          </el-tag>
      </template>
    </el-table-column>
      <el-table-column label="配送时间" align="center" prop="distributionTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.distributionTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="到达时间" align="center" prop="arriveTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.arriveTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="客户" align="center" prop="customerName" />
      <el-table-column label="客户联系人" align="center" prop="contactName" />
      <el-table-column label="客户联系电话" align="center" prop="contactNumber" />
      <el-table-column label="配送地址" align="center" prop="deliveryAddr" />
      <el-table-column label="发货地址" align="center" prop="sendAddr" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['carbon:distribution:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-truck"
            @click="handle1(scope.row)"
            v-hasPermi="['carbon:distribution:edit']"
            v-if="scope.row.distributionStatus=='2'"
          >配送</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-s-claim"
            @click="handle2(scope.row)"
            v-hasPermi="['carbon:distribution:edit']"
            v-if="scope.row.distributionStatus=='0'"
          >收货</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['carbon:distribution:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改配送管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="单据号" prop="orderId">
          <el-input v-model="form.orderId" placeholder="请输入单据号" />
        </el-form-item>
        <el-form-item label="调度单号" prop="dispatchId">
          <el-input v-model="form.dispatchId" placeholder="请输入调度单号" />
        </el-form-item>
        <el-form-item label="车牌号" prop="carNumber">
          <el-input v-model="form.carNumber" placeholder="请输入车牌号" />
        </el-form-item>
        <el-form-item label="司机" prop="driverName">
          <el-input v-model="form.driverName" placeholder="请输入司机" />
        </el-form-item>
        <el-form-item label="司机联系电话" prop="driverNumber">
          <el-input v-model="form.driverNumber" placeholder="请输入司机联系电话" />
        </el-form-item>
        <el-form-item label="配送时间" prop="distributionTime">
          <el-date-picker clearable
            v-model="form.distributionTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择配送时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="到达时间" prop="arriveTime">
          <el-date-picker clearable
            v-model="form.arriveTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择到达时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="客户" prop="customerName">
          <el-input v-model="form.customerName" placeholder="请输入客户" />
        </el-form-item>
        <el-form-item label="客户联系人" prop="contactName">
          <el-input v-model="form.contactName" placeholder="请输入客户联系人" />
        </el-form-item>
        <el-form-item label="客户联系电话" prop="contactNumber">
          <el-input v-model="form.contactNumber" placeholder="请输入客户联系电话" />
        </el-form-item>
        <el-form-item label="配送地址" prop="deliveryAddr">
          <el-input v-model="form.deliveryAddr" placeholder="请输入配送地址" />
        </el-form-item>
        <el-form-item label="发货地址" prop="sendAddr">
          <el-input v-model="form.sendAddr" placeholder="请输入发货地址" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input type="textarea" v-model="form.remark" placeholder="请输入备注" :disabled="this.type == '123'" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDistribution, updateStatus,updateStatus2,getDistribution, delDistribution, addDistribution, updateDistribution } from "@/api/wms/delivery";

export default {
  name: "Distribution",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 配送管理表格数据
      distributionList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderId: null,
        dispatchId: null,
        carNumber: null,
        driverName: null,
        driverNumber: null,
        distributionStatus: null,
        distributionTime: null,
        arriveTime: null,
        customerName: null,
        contactName: null,
        contactNumber: null,
        deliveryAddr: null,
        sendAddr: null
      },
      // 表单参数
      form: {
        distributionStatus:'0'
      },
      // 表单校验
      rules: {
        carNumber: [
          { required: true, message: "车牌号不能为空", trigger: "blur" }
        ],
        distributionTime: [
          { required: true, message: "配送时间不能为空", trigger: "blur" }
        ],
        deliveryAddr: [
          { required: true, message: "配送地址不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询配送管理列表 */
    getList() {
      this.loading = true;
      listDistribution(this.queryParams).then(response => {
        this.distributionList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        checkId: null,
        orderId: null,
        dispatchId: null,
        carNumber: null,
        driverName: null,
        driverNumber: null,
        distributionStatus: null,
        distributionTime: null,
        arriveTime: null,
        customerName: null,
        contactName: null,
        contactNumber: null,
        deliveryAddr: null,
        sendAddr: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.checkId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加配送管理";
    },
    /** 配送 */
    handle1(row){
      updateStatus(row.checkId ).then(res=>{
        this.$message.success(res.msg);
        this.getList();
      })
    },
    /** 收货 */
    handle2(row){
      updateStatus2(row.checkId ).then(res=>{
        this.$message.success(res.msg);
        this.getList();
      })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const checkId = row.checkId || this.ids
      getDistribution(checkId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改配送管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.checkId != null) {
            updateDistribution(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDistribution(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const checkIds = row.checkId || this.ids;
      this.$modal.confirm('是否确认删除配送管理编号为"' + checkIds + '"的数据项？').then(function() {
        return delDistribution(checkIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('carbon/distribution/export', {
        ...this.queryParams
      }, `distribution_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
